package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleSettingDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.security.Permission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleSettingService.class)
@Transactional
public class RoleSettingServiceImpl implements RoleSettingService {

    @Autowired
    private RoleSettingDao roleSettingDao;

    @Autowired
    private PermissionDao permissionDao;

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleSettingDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Role> result = page.getResult();
        return new PageResult(total, result);
    }

    //新增数据回显
    public Map findAll() {
        //创建map用于存放权限和菜单
        Map<String, List> map = new HashMap<>();
        //查询权限信息
        List<Permission> permissions = roleSettingDao.findAllPermissions();
        //查询菜单信息
        List<Menu> menus = roleSettingDao.findAllmenus();
        map.put("permissions", permissions);
        map.put("menus", menus);

        return map;
    }

    //新增角色，同时关联检查组
    public void add(Role role, Integer[] permissions, Integer[] menus) {
        roleSettingDao.add(role);
        Integer roleId = role.getId();   //获取角色id
        this.setRoleAndpermissions(roleId, permissions);  //用角色id设置权限
        this.setRoleAndmenus(roleId, menus);     //用角色id设置菜单
    }

    //编辑角色信息
    public void edit(Role role, Integer[] permissions, Integer[] menus) {
        Integer id = role.getId();
        if (id != null) {
            roleSettingDao.deletePermissions(id);
            roleSettingDao.deleteMenus(id);
        }
        //重新设置他们的关系
        this.setRoleAndpermissions(id, permissions);
        this.setRoleAndmenus(id, menus);
    }

    //编辑的数据回显
    public Map editecho(int id) {
        Role role = roleSettingDao.findRoleById(id);   //获取角色
        List<Menu> menus = roleSettingDao.findByRoleId(id);   //获取菜单
        List<com.itheima.pojo.Permission> permissions = permissionDao.findByRoleId2(id);   //获取权限
        List meunsArray = new ArrayList<>();  //创建菜单集合
        List permissionsArray = new ArrayList<>();  //创建权限集合
        for (Menu m : menus) {           //遍历菜单
            meunsArray.add(m.getId());   //获取菜单的id并放入菜单集合
        }
        for (com.itheima.pojo.Permission permission : permissions) {   //遍历权限
            permissionsArray.add(permission.getId());  //获取权限集合并把权限的id放入权限集合
        }
        Map map = new HashMap<String, Object>();   //创建map集合
        map.put("menus", meunsArray);           //把菜单和和菜单集合放进map集合
        map.put("permissions", permissionsArray); //把权限和权限集合放进map集合
        map.put("role", role);                   //把角色放进map集合
        return map;            //这样返回的毛集合中就有了用户的角色   权限  菜单
    }

    //删除角色
    public void delete(int id) {
    roleSettingDao.deletePermissions(id);
    roleSettingDao.deleteMenus(id);
    roleSettingDao.delete(id);
    }


    //设置角色和权限
    public void setRoleAndpermissions(Integer roleId, Integer[] permissions) {
        if (permissions != null && permissions.length > 0) {
            for (Integer permission : permissions) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", permission);
                roleSettingDao.setRoleAndpermissions(map);
            }
        }
    }

    //设置角色和菜单
    public void setRoleAndmenus(Integer roleId, Integer[] menus) {
        if (menus != null && menus.length > 0) {
            for (Integer menu : menus) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("menu_id", menu);
                roleSettingDao.setRoleAndmenus(map);
            }
        }
    }


}
